//
//  SAPRecomCarTableViewCell.m
//  CarsHome
//
//  Created by dapeng on 15/12/4.
//  Copyright © 2015年 dapeng. All rights reserved.
//

#import "SAPRecomCarTableViewCell.h"
#import "Theme-PrefixHeader.pch"
@implementation SAPRecomCarTableViewCell
- (void)dealloc {

    [super dealloc];
}
- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        
        [SAPNetWorkTool getWithUrl:recomCar parameter:nil httpHeader:nil responseType:ResponseTypeJSON   success:^(id result) {
            NSDictionary *dic = [result valueForKey:@"result"];
            self.twoModel = [[SAPFindCarTwoModel alloc] initWithDataSource:dic];
            [self imageLabel];
        } fail:^(NSError *error) {
            NSLog(@"%@", error);
        }];
  
    }return self;
}
- (void)layoutSubviews {
    [super layoutSubviews];
    for (int i = 0; i < self.twoModel.listArray.count; i++) {
        UIImageView *carsPic = (UIImageView *)[self viewWithTag:i + 100];
        UILabel *carsName = (UILabel *)[self viewWithTag:i + 200];
        carsPic.frame = CGRectMake(SCREEN_SIZE.width / 3 * i + 15, 10, 100, 60);
        carsName.frame = CGRectMake(SCREEN_SIZE.width / 3 * i + 15, 70, 100, 20);
        [self addSubview:carsPic];
        [self addSubview:carsName];
        
    
    }
    NSUserDefaults *user = [NSUserDefaults standardUserDefaults];
    NSString *passWord = [user objectForKey:@"nightModel"];
    
    if (!(passWord == nil) && [passWord isEqualToString:@"day"]) {
        self.backgroundColor = [UIColor colorWithWhite:0.126 alpha:1.000];
        self.textLabel.textColor = RGB(153.0,153.0,153.0,1.0f);
        
    }else{
        self.backgroundColor = [UIColor whiteColor];
        
    }
    
}
- (void)imageLabel {
    
    
    if (self.twoModel) {
        for (int i = 0; i < self.twoModel.listArray.count; i++) {
            UIImageView *carsPic = [[UIImageView alloc] initWithFrame:CGRectZero];
            UILabel *carsName = [[UILabel alloc] initWithFrame:CGRectZero];
            SAPFindCarThreeModel *three = self.twoModel.listArray[i];
            [carsPic sd_setImageWithURL:[NSURL URLWithString: three.img]];
            carsPic.userInteractionEnabled = YES;
            carsName.text = three.seriesname;
            carsPic.tag = i + 100;
            carsName.tag = i + 200;
            carsName.textAlignment = NSTextAlignmentCenter;
            carsName.font = [UIFont systemFontOfSize:12];
            [self addSubview:carsPic];
            [self addSubview:carsName];
            UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tapClickAction:)];
            [carsPic addGestureRecognizer:tap];
            NSUserDefaults *user = [NSUserDefaults standardUserDefaults];
            NSString *passWord = [user objectForKey:@"nightModel"];
            
            if (!(passWord == nil) && [passWord isEqualToString:@"day"]) {
                self.backgroundColor = [UIColor colorWithWhite:0.126 alpha:1.000];
                carsName.textColor = RGB(153.0,153.0,153.0,1.0f);
                
            }
       
            [tap release];
        }
        
    }
}

- (void)tapClickAction:(UITapGestureRecognizer *)tap {
    _count = tap.view.tag - 100;
    SAPFindCarThreeModel *three = self.twoModel.listArray[_count];
    NSString *str = [NSString stringWithFormat:@"http://comm.app.api.autohome.com.cn/static/sharejump.html?p=1&pm=1&go=1&cont=5&ourl=http://m.autohome.com.cn/%@/", three.seriesid];
    
    
    
    [self.delegate passValue2:str];

}


- (void)awakeFromNib {
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
